#ifndef CLIENT_WINDOWS_COMMON_AUTO_CRITICAL_SECTION_H__
#define CLIENT_WINDOWS_COMMON_AUTO_CRITICAL_SECTION_H__

#include <Windows.h>
#include <assert.h>

namespace longkey_breakpad
{

	// Automatically enters the critical section in the constructor and leaves
	// the critical section in the destructor.
	class AutoCriticalSection
	{
	public:
		// Creates a new instance with the given critical section object
		// and enters the critical section immediately.
		explicit AutoCriticalSection(CRITICAL_SECTION* cs) : cs_(cs)
		{
			assert(cs_);
			EnterCriticalSection(cs_);
		}

		// Destructor: leaves the critical section.
		~AutoCriticalSection()
		{
			LeaveCriticalSection(cs_);
		}

	private:
		// Disable copy ctor and operator=.
		AutoCriticalSection(const AutoCriticalSection&);
		AutoCriticalSection& operator=(const AutoCriticalSection&);

		CRITICAL_SECTION* cs_;
	};

}  // namespace longkey_breakpad

#endif  // CLIENT_WINDOWS_COMMON_AUTO_CRITICAL_SECTION_H__